﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using TimKen.DataAccess;
using System.Data;

namespace TimKen.BussLogic {


    /// <summary>
    /// 类Recommend。
    /// </summary>
    public class Recommend {
        public Recommend() { }
        #region Model
        private int _recommendid;
        private int _categoryid;
        private string _image;
        private string _descriptionen;
        private string _descriptioncn;
        private DateTime? _createdate;
        /// <summary>
        /// 
        /// </summary>
        public int RecommendID {
            set { _recommendid = value; }
            get { return _recommendid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int CategoryID {
            set { _categoryid = value; }
            get { return _categoryid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Image {
            set { _image = value; }
            get { return _image; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string DescriptionEn {
            set { _descriptionen = value; }
            get { return _descriptionen; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string DescriptionCn {
            set { _descriptioncn = value; }
            get { return _descriptioncn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? CreateDate {
            set { _createdate = value; }
            get { return _createdate; }
        }
        #endregion Model


        #region  成员方法

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Recommend(int RecommendID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecommendID,CategoryID,Image,DescriptionEn,DescriptionCn,CreateDate ");
            strSql.Append(" FROM Recommend ");
            strSql.Append(" where RecommendID=@RecommendID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RecommendID", SqlDbType.Int,4)};
            parameters[0].Value = RecommendID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["RecommendID"].ToString() != "") {
                    RecommendID = int.Parse(ds.Tables[0].Rows[0]["RecommendID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "") {
                    CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                Image = ds.Tables[0].Rows[0]["Image"].ToString();
                DescriptionEn = ds.Tables[0].Rows[0]["DescriptionEn"].ToString();
                DescriptionCn = ds.Tables[0].Rows[0]["DescriptionCn"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "") {
                    CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RecommendID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Recommend");
            strSql.Append(" where RecommendID=@RecommendID ");

            SqlParameter[] parameters = {
					new SqlParameter("@RecommendID", SqlDbType.Int,4)};
            parameters[0].Value = RecommendID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add() {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Recommend(");
            strSql.Append("CategoryID,Image,DescriptionEn,DescriptionCn,CreateDate)");
            strSql.Append(" values (");
            strSql.Append("@CategoryID,@Image,@DescriptionEn,@DescriptionCn,@CreateDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@DescriptionEn", SqlDbType.NVarChar,3000),
					new SqlParameter("@DescriptionCn", SqlDbType.NVarChar,3000),
					new SqlParameter("@CreateDate", SqlDbType.DateTime,3)};
            parameters[0].Value = CategoryID;
            parameters[1].Value = Image;
            parameters[2].Value = DescriptionEn;
            parameters[3].Value = DescriptionCn;
            parameters[4].Value = CreateDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null) {
                return 1;
            } else {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update() {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Recommend set ");
            strSql.Append("CategoryID=@CategoryID,");
            strSql.Append("Image=@Image,");
            strSql.Append("DescriptionEn=@DescriptionEn,");
            strSql.Append("DescriptionCn=@DescriptionCn,");
            strSql.Append("CreateDate=@CreateDate");
            strSql.Append(" where RecommendID=@RecommendID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RecommendID", SqlDbType.Int,4),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@Image", SqlDbType.VarChar,64),
					new SqlParameter("@DescriptionEn", SqlDbType.NVarChar,3000),
					new SqlParameter("@DescriptionCn", SqlDbType.NVarChar,3000),
					new SqlParameter("@CreateDate", SqlDbType.DateTime,3)};
            parameters[0].Value = RecommendID;
            parameters[1].Value = CategoryID;
            parameters[2].Value = Image;
            parameters[3].Value = DescriptionEn;
            parameters[4].Value = DescriptionCn;
            parameters[5].Value = CreateDate;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int RecommendID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Recommend ");
            strSql.Append(" where RecommendID=@RecommendID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RecommendID", SqlDbType.Int,4)};
            parameters[0].Value = RecommendID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int RecommendID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RecommendID,CategoryID,Image,DescriptionEn,DescriptionCn,CreateDate ");
            strSql.Append(" FROM Recommend ");
            strSql.Append(" where RecommendID=@RecommendID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RecommendID", SqlDbType.Int,4)};
            parameters[0].Value = RecommendID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["RecommendID"].ToString() != "") {
                    RecommendID = int.Parse(ds.Tables[0].Rows[0]["RecommendID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "") {
                     CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
               Image = ds.Tables[0].Rows[0]["Image"].ToString();
                 DescriptionEn = ds.Tables[0].Rows[0]["DescriptionEn"].ToString();
                 DescriptionCn = ds.Tables[0].Rows[0]["DescriptionCn"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "") {
                     CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Recommend ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

 
 

         public DataSet GetList(int languageid) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  PCategory.CategoryName,Recommend.*  From  Recommend left outer join PCategory on  Recommend.CategoryID=PCategory.CategoryID");
            strSql.Append(" WHERE PCategory.LanguageID =  " + languageid);
          
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  成员方法
    }
}

